﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ruscehproj2
{
    public partial class IKO : Form
    {
       public IKO()
        {
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);    
            InitializeComponent();
        }

        private Timer timer = new Timer();
        int angle = 0;
        int x1 = 170;
        int y1 = 178;
       
        public class target
        {
            int real_r;
            int r;
            int angle;
            bool sw;
            int count=0;
            
            public target(int a, int r1)
            {
                 angle = a;
                 r = r1;
                 real_r = r1;
                
            }

            public int get_r()
            {
                return r;
            }
            public int get_real_r()
            {
                return real_r;
            }
            public void set_r(int r1)
            {
                r = r1;
                real_r = r1;
            }
            public int get_angle()
            {
                return angle;
            }
            public void set_angle(int angle1)
            {
                angle = angle1;
            }
           
            
                                public void move( int speed)
            {
                Random rnd = new Random();

                int rand = rnd.Next(0, 30);
                int rand180 = rnd.Next(0, 180);

                count++;
                if (count == speed)
                {
                    if (sw) r--;

                    if (!sw)  r++;
                
                if (r == 0) { angle = angle + rand + 180; sw = !sw; }
              //  if (r1 > 104) { angle1 = rand180; sw = !sw;  }
                while (angle < 0) angle++;
                count = 0;
                }
            }
                            public void draw(int i, Graphics gg)
          {
            if (angle >= i * 30 && angle <= i * 30 + 40 && r<104) 
            gg.DrawEllipse(new Pen(Brushes.Red, 3), Convert.ToInt32(170 + r * Math.Cos(3.14 * angle / 180)), Convert.ToInt32(178 + r * Math.Sin(3.14 * angle / 180)), 2, 2);
          }
                            public void mode(bool m)
                            {
                                if (!m) r= (r / 4);
                                if (m) r= r * 4;
                            }
        }
        target T1 = new target(170, 90);
        target T2 = new target(100, 40);
        public void timer1_Tick(object sender, EventArgs e)
        {


           
           // while (r1  0) r1++;
          //System.Media.SoundPlayer sp = new System.Media.SoundPlayer("error.wav");
            T1.move(30);
            T2.move(20);
            Graphics g = this.CreateGraphics();
            Graphics gg = this.CreateGraphics(); 
            Matrix _mirrorMatrix = new Matrix();
                 _mirrorMatrix.Translate(x1, y1);  // перенос начала координат в первую точку прямой отражения
                 _mirrorMatrix.Rotate(angle); // поворот на угол между прямой отражения и ОХ
                 Matrix mirror = new Matrix(1, 0, 0, -1, 0, 0); // матрица отражения
                _mirrorMatrix.Multiply(mirror);  // обратный поворот
                _mirrorMatrix.Rotate(-angle); // обратный перенос
                _mirrorMatrix.Translate(-x1, -y1);
                
            g.Transform = _mirrorMatrix;
            angle = angle + 6;
            g.Transform = _mirrorMatrix;
            g.DrawLine(new Pen(Brushes.GreenYellow, 3), Convert.ToInt32(170 + 104 * Math.Cos((0) * 3.14 / 6)), Convert.ToInt32(178 + 104 * Math.Sin((0) * 3.14 / 6)), x1, y1);
            int w = Convert.ToInt32(150 + 104 * Math.Cos(3.14 / 6));
            if (angle == 180) angle = 0;
            Rectangle quadrat = new Rectangle(95, 97, 150, 150);
            int q = (angle * 2);
              
            for (int i = 0; i <= 12; i++)
            {
                if (q >= i * 30 && q < i * 30 + 40)
                {
                    gg.DrawLine(new Pen(Brushes.GreenYellow, 1), Convert.ToInt32(170 + 104 * Math.Cos((i) * 3.14 / 6)), Convert.ToInt32(178 + 104 * Math.Sin((i) * 3.14 / 6)), x1, y1);
                    gg.DrawArc(new Pen(Brushes.GreenYellow, 1), quadrat, (i * 30), 40);
                    T1.draw(i, gg);
                    T2.draw(i, gg);
                         }
            }    
        }

     
        private void timer2_Tick(object sender, EventArgs e)
        { 
             label1.Text = T1.get_angle().ToString();
         label2.Text = T1.get_real_r().ToString();
           
              this.BackgroundImage = Resources.display;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            {
              //  T1.set_angle(Convert.ToInt32(textBox1.Text));
                while (T1.get_angle() > 360) T1.set_angle(T1.get_angle() - 360);
                label1.Text = T1.get_angle().ToString();
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && (e.KeyChar < 48 || e.KeyChar > 57))
                e.Handled = true;  
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {           
            if (e.KeyChar != 8 && (e.KeyChar < 48 || e.KeyChar > 57))
                e.Handled = true;  
        }

        private void button3_Click(object sender, EventArgs e)
        {
           
           // T1.set_r(Convert.ToInt32(textBox2.Text));
            if (T1.get_r() > 104) T1.set_r(300);
            label2.Text = T1.get_r().ToString();
        }

        private void IKO_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) IKO.ActiveForm.Close();  
        }
        bool i=true;
        public void button2_Click_1(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
            timer2.Enabled = !timer2.Enabled;
            i = !i;
            if (i) { button2.BackgroundImage = Resources.sw1; T1.mode(true); T2.mode(true); }
            else { button2.BackgroundImage = Resources.sw2; T1.mode(false); T2.mode(false); }
          
        }
    }
}
